import getopt
import sys
import config
def usage(defaults_map):    
    usage_str = 'python %s' % sys.argv[0]
    for k,v in defaults_map.iteritems():
        usage_str += ' --%s=[%s]' % (k,v)
    print >>sys.stderr, usage_str 
def parse_opt(defaults_map):
    try:
        opt_strings = ['%s=' % k for k in defaults_map.keys()] + ['help']        
        opts, args = getopt.getopt(sys.argv[1:], '', opt_strings)    
    except getopt.GetoptError, err:
        # print help information and exit:
        print str(err) # will print something like "option -a not recognized"
        usage(defaults_map)
        sys.exit(2)
    args = defaults_map.copy()    
    for o, a in opts:
        if o == '--help':
            usage(defaults_map)
            sys.exit(0)
        if o in ('--http_port','--rpc_port', '--rs_port', '--tracker_port', '--external_port'):
            a = int(a)
        args[o[2:]] = a
    return args